import json

from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render, render_to_response


# Create your views here.
from Common import sql_content
from AdminManagement.WebDataControl import dataDetails

def AdminMid(request):
    # # print("中间件传递过来的"+name)
    # # # name = 'indexAdmin.html'
    # print(name)
    # print("我去看看啊：",request.session.get('LoginName_session',None))
    if request.session.get('LoginName_session',None) is not None:
        if (request.session.get('LoginName_session',None) == "admin" or request.session.get('LoginName_session',None) == "方振"):
            return
        else:
            return HttpResponseRedirect('/zfq/')
    elif request.session.get('LoginName_session',None) is None :
        return HttpResponseRedirect('/zfq/login/')

def AdminLay(request):

    return render_to_response('page/main.html')

def index(request):
    return render_to_response('AdminIndex.html')

def jobMessage(request):
    return render_to_response('jobMessageAdmin.html')

def DataDetails(request):
    if request.method == "GET":
        data_count = sql_content.sql_content().data_count("select count(*) from data_collection.data_51job_she;")
        page_size = 50
        now_page = 1
        page_begin = 1
        page_end = 50
        end_page_begin = 0
        if (data_count % page_size == 0):
            page_count = data_count / page_size
        else:
            page_count = data_count // page_size + 1
            end_page_begin = data_count // page_size * page_size + 1
            # print(page_count,end_page_begin)
        if(request.GET.get('hidPageIndex') is not None):
            now_page = int(request.GET.get('hidPageIndex'))
            page_end = now_page * page_size
            page_begin = page_end - page_size + 1
            if(page_end > data_count):
                page_end = data_count
        page_count_list = []
        for num in range(1,page_count+1):
            page_count_list.append(num)
        print(page_begin,page_end)
        analysis_fill = dataDetails._DataDetails().webDataManage_Admin(page_begin,page_end)
        page_meg = {'page_count':page_count , 'page_count_list':page_count_list,'now_page':now_page}
        print('views_analysis_fill_modth', analysis_fill)
        return render_to_response('DataDetails_zhil.html', {'Analysis': analysis_fill, 'page_meg':page_meg})
    elif request.method == "POST":
        print('this is Post!')
        now_id = int(request.POST.get('hidDataDetail'))
        if (now_id is not None):
            detail_fill = dataDetails._DataDetails().analysis_sql_id(now_id)
            return render_to_response('DataDetails_zhil.html', {'detail_fill':detail_fill})

    else:
        return render_to_response('DataDetails_zhil.html')


#网站数据管理->爬取数据概图
def DataChart(request):
    return render_to_response("WebDataChart.html")
def DataChart_lagou(request):
    return render_to_response("WebDataChart_lagou.html")
#网站数据管理->爬取数据详情表
def DataDetails_zhil(request):
    if request.method == "GET":
        return DataDetails_common(request,"data_51job_she",1,"DataDetails_zhil")
    elif request.method == "POST":
        print('this is Post!')
        now_id = int(request.POST.get('hidDataDetail'))
        if (now_id is not None):
            detail_fill = dataDetails._DataDetails().analysis_sql_id(now_id)
            return render_to_response('DataDetails_zhil.html', {'detail_fill': detail_fill})
    else:
        return render_to_response('DataDetails_zhil.html')
def DataDetails_xiaom(request):
    return DataDetails_common(request,"data_xiaomi",4,"DataDetails_xiaom")
def DataDetails_51j(request):
    return DataDetails_common(request,"data_51job_she",6,"DataDetails_51j")
def DataDetails_lag(request):
    return DataDetails_common(request,"data_lagou",6,"DataDetails_lag")
def DataDetails_common(request,database,method,page):
        data_count = sql_content.sql_content().data_count("select count(*) from data_collection."+database+";")
        page_size = 50
        now_page = 1
        page_begin = 1
        page_end = 50
        end_page_begin = 0
        if (data_count % page_size == 0):
            page_count = data_count / page_size
        else:
            page_count = data_count // page_size + 1
            end_page_begin = data_count // page_size * page_size + 1
            # print(page_count,end_page_begin)
        if(request.GET.get('hidPageIndex') is not None):
            now_page = int(request.GET.get('hidPageIndex'))
            page_end = now_page * page_size
            page_begin = page_end - page_size + 1
            if(page_end > data_count):
                page_end = data_count
        page_count_list = []
        for num in range(1,page_count+1):
            page_count_list.append(num)
        print(page_begin,page_end)
        analysis_fill = dataDetails._DataDetails().webDataManage_Admin(page_begin,page_end,method)
        page_meg = {'page_count':page_count , 'page_count_list':page_count_list,'now_page':now_page}
        print('views_analysis_fill_modth', analysis_fill)
        return render_to_response(page+'.html', {'Analysis': analysis_fill, 'page_meg':page_meg})